<template>
  <el-dialog title="添加圖庫" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form size="small" :model="form" :rules="formRules" ref="form">
      <el-form-item label="所屬圖庫" :label-width="formLabelWidth">
        <el-select v-model="form.parentId">
          <el-option label="頂級圖庫" value="0"></el-option>
          <el-option :value="cat.categoryId" :label="cat.name" :key="cat.categoryId" v-for="cat in addform.catList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="圖庫名稱" prop="name" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="分類圖片" prop="image" :label-width="formLabelWidth">
        <el-row>
          <el-button type="primary" @click="openUpload">選擇圖片</el-button>
          <div v-if="form.image!=''" class="img">
            <img :src="filePath" width="100" height="100" />
          </div>
        </el-row>
        <div class="gray9">建議圖片尺寸爲140x114</div>
      </el-form-item>
      <el-form-item label="圖庫排序" prop="sort" :label-width="formLabelWidth">
        <el-input v-model.number="form.sort" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
    <div  class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" @click="addUser" :loading="loading">確 定</el-button>
    </div>
    </template>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">上傳圖片</Upload>
  </el-dialog>
</template>

<script>
import SystemApi from '@/api/system.js';
import Upload from '@/components/file/Upload.vue';
export default {
  components: {
    Upload
  },
  data() {
    return {
      form: {
        parentId: '0',
        name: '',
        sort: 100,
        image: ''
      },
      formRules: {
        name: [{
          required: true,
          message: "請輸入圖庫名稱",
          trigger: 'blur'
        }],
        image: [{
          required: true,
          message: "請上傳圖片",
          trigger: 'blur'
        }],
        sort: [{
          required: true,
          message: "圖庫排序不能爲空"
        }, {
          type: 'number',
          message: "圖庫排序必須爲數字"
        }]
      },
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      /*是否上传图片*/
      isupload: false
    };
  },
  props: ['open_add', 'addform'],
  created() {
    this.dialogVisible = this.open_add;
  },
  methods: {
    /*添加用户*/
    addUser() {
      let self = this;
      let params = self.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          SystemApi.AddPicture(params).then(data => {
            self.loading = false;
            ElMessage({
              message: "添加成功",
              type: 'success'
            });
            self.dialogFormVisible(true);
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        });
      }
    },
    /*上传*/
    openUpload(e) {
      this.type = e;
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      if (e != null && e.length > 0) {
        this.filePath = e[0].filePath;
        this.form.image = e[0].filePath;
      }
      this.isupload = false;
    }
  }
};
</script>

<style>
.img {
    margin-top: 10px;
  }
</style>

